﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using Charities.ModelViews;
using System.IO;

namespace Charities
{
    public partial class GrantsReport : Form
    {
        int userId;
        int worthId;
        public bool is_loaded = false;
        public GrantsReport(int uid, int wid,Form m)
        {
            fo = m;
            this.MdiParent = m;
            userId = uid;
            worthId = wid;
            InitializeComponent();
        }
        public GrantsReport(Form m)
        {
            fo = m;
            this.MdiParent = m;
            InitializeComponent();
        }
        Form fo = null;
        ModelContainer ctx = new ModelContainer();
        private void GrantsReport_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            if (worthId > 0)
            {
                ModelViews.WorthiesView worthyView = new ModelViews.WorthiesView();
                Worthy worthy = ctx.Worthies.Single(c => c.Id == worthId);
                Person person = ctx.People.Single(p => p.Id == worthy.PersonId);
                worthyView.DisplayName = person.FirstName + " " + person.FatherName + " " + person.LastName;
                worthyView.CurrentAddress = worthy.CurrentAddress;
                worthyView.OreginalAddress = worthy.OriginalAddress;
                worthyView.Inspector = worthy.Inspector.Name;
                worthyView.needs = worthyView.NeedsView(worthy.Needs.ToList());
                worthyView.PhoneNumber = worthy.Phone;
                worthyView.WorthyTypeName = worthy.WorthyType.Name;
                worthyView.insertDate = worthy.InsertDate;
                worthyView.Id = worthy.Id;
                worthyView.RelativesCount = ctx.People.Where(r => r.WorthyId == worthId).Count();

                List<ModelViews.GrantView> gviews = new List<ModelViews.GrantView>();
                foreach (var grant in worthy.Grants)
                {

                    if (grant.CartId != null)//cart
                    {
                        ModelViews.GrantView gv = new ModelViews.GrantView();
                        gv.issuDate = grant.IssueDate;
                        gv.issuNum = grant.IssueNumber;
                        if(grant.Schedules.Count()>0)
                        gv.name = " (مجدولة) "+grant.Cart.Name+" بتاريخ "+grant.Schedules.Single(c=>c.GrantId==grant.Id).NextGrantDate.ToShortDateString();
                        else if(grant.IsDelivered==true)
                            gv.name = " (مسلمة) "+grant.Cart.Name;
                        else if(grant.IsDelivered!=true)
                            gv.name = " (غير مسلمة) " + grant.Cart.Name;
                        else
                            gv.name = grant.Cart.Name;
                        var gvammount = grant.GrantDetails.Where(c => c.GrantId == grant.Id).FirstOrDefault();
                        if (gvammount != null)
                        {
                            gv.count = gvammount.Amount;
                            gviews.Add(gv);
                        }
                    }
                    else//lonly supply
                    {
                        var gvammount = grant.GrantDetails.Where(c => c.GrantId == grant.Id && c.SupplyId != null).ToList();
                        foreach (var gr in gvammount)
                        {
                            ModelViews.GrantView gvw = new ModelViews.GrantView();
                            gvw.issuDate = grant.IssueDate;
                            gvw.issuNum = grant.IssueNumber;
                            if(gr.Grant.Schedules.Count>0)
                                gvw.name = " (مجدولة) "+gr.Supply.Name+" بتاريخ "+gr.Grant.Schedules.Single(c=>c.GrantId==gr.GrantId).NextGrantDate.ToShortDateString();
                            else if(gr.Grant.IsDelivered==true)
                                gvw.name = " (مسلمة) "+gr.Supply.Name;
                            else if (gr.Grant.IsDelivered!=true)
                                gvw.name = "  (غير مسلمة) " + gr.Supply.Name;
                            else
                                gvw.name = gr.Supply.Name;
                            gvw.count = gr.Amount;
                            gviews.Add(gvw);
                        }

                    }
                }
                if (ctx.CharityInfos.Count() > 0)
                {
                    CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
                }
                else
                {
                    MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                WorthiesViewBindingSource.DataSource = worthyView;
                GrantViewBindingSource.DataSource = gviews;

                this.GrantsReportViwer.RefreshReport();
            }
            else
            {

            }
        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        private void GrantsReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void GrantsReport_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
